
function Tree (root){
    this.root = new Node(root);
    this.count = 1;

    this.getRoot = function(){
        return this.root;
    };

    this.getParent = function(node){
        return node.getParent();
    };
    
     this.getChildren = function(node){
        return node.getChildren();
    };
    
    this.getSize = function(){
        return this.count;
    };
    
    this.swapElements = function(node1,node2)
    {
        var element = node1.get();
        node1.set(node2.get());
        node2.set(element);
    };

    this.printChildren = function(node){
        var array = new Array();
        var children = node.getChildren();
        for (var i = 0; i < node.childrenSize(); i++){
            array.push(node.getChild(i).get());
        }
        return array;
    };
    
    this.listElements2 = function(node, array){
        array.push(node.get());
        for (var i = 0; i<node.childrenSize(); i++){
            this.listElements2(node.getChild(i), array);
        }
        return (array);
    };
    
    this.getNodes2 = function(node, array){
        array.push(node);
        for (var i = 0; i<node.childrenSize(); i++){
            this.getNodes2(node.getChild(i), array);
        }
        return (array);
    };
  
    this.getNodes = function(node){
        return this.getNodes2(node, new Array());
    };
    
    this.listElements = function(node){
        return this.listElements2(node, new Array());
    };

    this.addChild = function(node, element){
        this.count++;
        return node.addChild(new Node(element));
    };

    this.replaceElement = function(node, value)
    {
        var old = node.get();
        node.set(value);
        return old;
    };


    this.showHtml = function(node)
    {        
        if(node == this.getRoot())
        {
            x = '<ul id="tree" style="display:none;">';
            x += '<li>'+ node.get();
        }
        else
        {
            x += '<li>'+ node.get();
        }
        
        if(node.childrenSize() > 0)
        {
            x += '<ul>';        
                       
            for(var i = 0; i < node.childrenSize(); i++)
            {
                this.showHtml(node.getChild(i));
            }
            x += '</ul>';    
        }
        return x;
    };
    
    this.isRoot = function(node)
    {
        return this.getRoot() == node;
    };
    
    this.isExternal = function(node)
    {
        return (node.getChildren().length == 0);
    };
    
    this.isInternal = function(node)
    {
        return (node.getChildren().length >= 1);
    };
    
    this.removeExternal = function(node)
    {
        if(tree.isExternal(node))
        {
            var father = node.getParent();

            for(var i = 0;  i < father.getChildren().length; i++)
            {
                if(father.getChildren()[i] == node)
                {
                    father.getChildren().splice(i,1);
                }
            }
        }
    };

}

function Node (element){
    this.parent = null;
    this.element = element;
    this.children = new Array();

    this.getChild = function(index){
        return this.children[index];
    };

    this.getChildren = function(){
        return this.children;
    };

    this.childrenSize = function(){
        return this.children.length;
    };

    this.addChild = function(child){
        this.children.push(child);
        child.setParent(this);     
        return child;
    };

    this.setParent = function(parent){
        this.parent = parent;
    };

    this.getParent = function(){
        return this.parent;
    };

    this.get = function() {
        return this.element;
    };

    this.set = function(value) {
        this.element = value;
    };


}


